/*
 * @Author: 胡海
 * @Date: 2020-04-02 21:13:04
 * @LastEditors: 胡海
 * @LastEditTime: 2020-04-06 23:09:24
 * @Description: 
 */
const db = wx.cloud.database()
Page({
   data: {
      orders: [],
      selected: true,
      selectedAll: true,
      totalPrice: 0,
      address: [],
      picImg: '../../images/mony.jpg',
      isPicImg: false,
   },
   onLoad: function () {

   },
   onShow: function () {
      this.loadOrders();
      this._addRess();
   },
   loadOrders: function () { //加载购物车商品订单信息
      let goodList = wx.getStorageSync('goodList');
      if (!goodList) {
         this.setData({
            orders: []
         })
         return
      }
      let orders = goodList.map(item => {
         return {
            ...item,
            buyNum: 1
         }
      })
      this.setData({
         orders: orders
      });
      this._nunTotalPrice()
   },
   _nunTotalPrice() { // 计算总价
      let totalPrice = 0;
      for (let i = 0; i < this.data.orders.length; i++) {
         let order = this.data.orders[i];
         totalPrice += order.price * order.buyNum;
      }
      this.setData({
         totalPrice: totalPrice
      });
   },
   _addRess() { // 收获地址
      let p = wx.getStorageSync('addresses');
      if (p) {
         this.setData({
            address: p
         })
      }
   },
   addGoods: function (e) { //添加商品
      let id = e.currentTarget.id
      let list = this.data.orders.map(item => {
         if (item.id == id) {
            item.buyNum++
         }
         return item
      })
      this.setData({
         orders: list
      })
      this._nunTotalPrice()
   },

   minusGoods: function (e) { //减少商品
      let id = e.currentTarget.id
      let list = this.data.orders.map(item => {
         if (item.id == id) {
            if (item.buyNum > 0) {
               item.buyNum--
            }
         }
         return item
      })
      this.setData({
         orders: list
      })
      this._nunTotalPrice()
   },
   submitO: function (e) { //提交订单
      let obj = {
         goodList: this.data.orders,
         totalPrice: this.data.totalPrice,
         address: this.data.address[0],
         name: this.data.orders[0].name
      }
      if (!obj.address) {
         wx.showToast({
            title: '请填写地址',
         })
         return
      }
      db.collection('shopCart').add({
         data: obj
      }).then((res) => {
         wx.showToast({
            title: '购买成功',
         })
         wx.removeStorageSync('goodList');
         this.setData({
            isPicImg: true
         })
      })

   },
   close: function (e) {
      wx.switchTab({
         url: '../index/index'
      })
      this.setData({
         isPicImg: false
      })
      this.loadOrders();
   },

   checkboxChange: function (e) { //商品行内复选框
      let ids = e.detail.value;
      if (ids.length == 0) {
         this.setData({
            selectedAll: false
         });
      } else {
         this.setData({
            selectedAll: true
         });
      }
   },
   checkAll: function (e) { //全选复选框
      var selected = this.data.selected;
      var result = selected == true ? false : true;
      this.setData({
         selected: result
      });
      if (result == false) {
         this.setData({
            totalPrice: 0
         });
         this.setData({
            selectedAll: false
         });
      } else {
         this.loadOrders();
         this.setData({
            selectedAll: true
         });
      }

   },



   selectAddress: function () { //选择收货地址
      wx.navigateTo({
         url: '../address/address'
      })
   }
})